Support a reusable 'var_cache' within the IdentifyVariableVisitor#3966
Support a reusable 'var_cache' within the IdentifyVariableVisitor#3966jsiirola wants to merge 7 commits into
IdentifyVariableVisitor#3966Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3966 +/- ##
==========================================
+ Coverage 90.11% 90.12% +0.01%
==========================================
Files 909 909
Lines 108580 108645 +65
==========================================
+ Hits 97847 97921 +74
+ Misses 10733 10724 -9
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
IdentifyVariableVisitorIdentifyVariableVisitor
| assert not self._expr_stack | ||
| self._seen = {} | ||
| if self._seen is None: | ||
| self._seen = {} |
There was a problem hiding this comment.
Why not use a component set?
There was a problem hiding this comment.
Mostly for historical reasons? I think that identify_variables actually predated the modern implementation of ComponentSet, and I was trying to do a minimal change here.
We should consider reworking the implementation to be based on ComponentSet. There might be a slight overhead increase (I am guessing the cost of an extra function call per leaf node) - we should benchmark that. (Although I am not expecting it to show up, as the core things like model generation, compilation, and writers do not use it).
There was a problem hiding this comment.
A quick test with PMedian (test8) shows that using a ComponentSet within identify_variables is 10-12% slower. One possibility is that we could advertise that the walker takes a ComponentSet, but internally we operate directly on the underlying _data dictionary?
There was a problem hiding this comment.
I like the idea of using a ComponentSet but operating on the underlying _data dictionary. That is provides a cleaner interface without the performance hit.
Fixes # .
Summary/Motivation:
This implements a minor performance improvement when we want to collect all the variables used by a model. The previous implementation ended up using two "seen" caches: one within the IdentifyVariableVisitor, and the other in the
get_var_from_components()utility. This allows a client to instantiate an IdentifyVariableVisitor with a persistent "seen" cache, thereby eliminating the redundant caches.This implementation is fully backwards compatible with the previous
IdentifyVariableViaitor/identify_variablesimplementation.Changes proposed in this PR:
IdentifyVariableVisitorget_vars_from_componentsto use a single persistent "seen" cacheget_vars()utility that is an alias forget_vars_from_components(ctype=(Constraint, Objective))AI-Use Disclosure
or
AI tools contributed to the development of this PR
Review process (select ONE):
Notes for reviewers (optional):
Legal Acknowledgement
By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution: